﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2014-3-11
/// Purpose: 		安全性配置节
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2015-3-26       防止SecurityConfigSection为空
/// Robin           2015-12-1       增加LogActionLevel
/// ****************************************************************************
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace RB.Web.Security
{
    /// <summary>
    /// 安全性配置节
    /// 用于取代VarAuthenticationSection和VarAuthorizationSection
    /// </summary>
    public class SecurityConfigSection : ConfigurationSection
    {
        public static SecurityConfigSection Current
        {
            get
            {
                var result = (SecurityConfigSection)ConfigurationManager.GetSection("RB.Security");
                if (result == null)
                    result = new SecurityConfigSection();
                return result;
            }
        }

        [ConfigurationProperty("authenticateServiceType", IsRequired = true)]
        public string AuthenticateServiceType
        {
            get
            {
                return (string)this["authenticateServiceType"];
            }
            set
            {
                this["authenticateServiceType"] = value;
            }
        }

        [ConfigurationProperty("loginUrl", DefaultValue = "/Account/LogOn", IsRequired = false)]
        public string LoginUrl
        {
            get
            {
                if (this.Properties.Contains("loginUrl"))
                    return (string)this["loginUrl"];
                return "";
            }
            set
            {
                this["loginUrl"] = value;
            }
        }

        [ConfigurationProperty("accessDeniedUrl", IsRequired = false)]
        public string AccessDeniedUrl
        {
            get
            {
                if (this.Properties.Contains("accessDeniedUrl"))
                    return (string)this["accessDeniedUrl"];
                return "";
            }
            set
            {
                this["accessDeniedUrl"] = value;
            }
        }

        public string CookieName
        {
            get
            {
                return ".RBAUTH-" + this.AppCode;
            }
        }

        [ConfigurationProperty("authorizeServiceType", IsRequired = true)]
        public string AuthorizeServiceType
        {
            get
            {
                return (string)this["authorizeServiceType"];
            }
            set
            {
                this["authorizeServiceType"] = value;
            }
        }

        [ConfigurationProperty("appCode", IsRequired = false)]
        public string AppCode
        {
            get
            {
                if (this.Properties.Contains("appCode"))
                    return (string)this["appCode"];
                return "";
            }
            set
            {
                this["appCode"] = value;
            }
        }

        [ConfigurationProperty("logActionLevel", IsRequired = false, DefaultValue = 1)]
        public int LogActionLevel
        {
            get
            {
                if (this.Properties.Contains("logActionLevel"))
                {
                    if (this["logActionLevel"] is int)
                    {
                        return (int)this["logActionLevel"];
                    }
                }
                return 1;
            }
            set
            {
                this["logActionLevel"] = value;
            }
        }
    }
}
